package com.jonlin.springboottest.controller;

import com.jonlin.springboottest.utils.excel.OutputExcelUtil;
import com.jonlin.springboottest.vo.TestVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TestController 测试类
 *
 * @author Administrator
 * @date 2020/4/5
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {
    @GetMapping("/test")
    public String test(String name){
        return "hello," + name;
    }


    /**
     * 测试下载
     * @param response 响应
     * @return
     */
    @GetMapping("/download")
    public ResponseEntity<Map<String, String>> download(HttpServletResponse response){
        List<TestVO> list = new ArrayList<>();
        TestVO TestVO1 = new TestVO("邹琳", 22);
        TestVO TestVO2 = new TestVO("邹琳2", 23);
//        list.add(TestVO1);
//        list.add(TestVO2);

        // 调用构造方法直接导出输出文件流
        try {
            new OutputExcelUtil("test.xlsx", list, TestVO.class, response);
        }catch (Exception e){
            // 异常则返回json
            e.printStackTrace();
            Map<String, String> result = new HashMap<>();
            result.put("msg", "测试下载失败");
            return new ResponseEntity<Map<String, String>>(result, HttpStatus.OK);
        }
        return null;
    }
}
